package client.ui;

import businessServer.ContactManager;
import dbServer.db.Contact;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class MainFrame extends JFrame {
    private ContactManager contactManager;
    private DefaultListModel<String> listModel;
    private JList<String> contactList;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;

    public MainFrame() {
        contactManager = new ContactManager();
        listModel = new DefaultListModel<>();
        contactList = new JList<>(listModel);
        addButton = new JButton("添加联系人");
        editButton = new JButton("修改联系人");
        deleteButton = new JButton("删除联系人");

        setLayout(new BorderLayout());
        add(new JScrollPane(contactList), BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);
        add(buttonPanel, BorderLayout.SOUTH);

        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddContactDialog dialog = new AddContactDialog(MainFrame.this);
                dialog.setVisible(true);
                refreshContactList();
            }
        });

        editButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String selectedContact = contactList.getSelectedValue();
                if (selectedContact != null) {
                    EditContactDialog dialog = new EditContactDialog(MainFrame.this, selectedContact);
                    dialog.setVisible(true);
                    refreshContactList();
                } else {
                    JOptionPane.showMessageDialog(MainFrame.this, "请先选择一个联系人进行修改");
                }
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String selectedContact = contactList.getSelectedValue();
                if (selectedContact != null) {
                    contactManager.deleteContact(selectedContact);
                    refreshContactList();
                } else {
                    JOptionPane.showMessageDialog(MainFrame.this, "请选择一个联系人进行删除！");
                }
            }
        });

        refreshContactList();

        setTitle("个体通讯录系统");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public void refreshContactList() {
        listModel.clear();
        List<Contact> contacts = contactManager.getAllContacts();
        for (Contact contact : contacts) {
            listModel.addElement(contact.getName());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
}

